package com.changgou.goods.controller;

import com.github.pagehelper.Page;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-08-22 0:51
 */
@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

/*
* 查询品牌列表
* */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList);

    }


    /*
    * 根据id查询品牌
    * */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    /*
    * 新增品牌
    *
    * */
    @PostMapping
    public Result addBrand(@RequestBody Brand brand){
                    brandService.addBrand(brand);
                    return  new Result(true,StatusCode.OK,"添加成功");
    }

    /*
    * 修改品牌
    * */
    @PutMapping(value = "/{id}")
    public Result updateBrand(@RequestBody Brand brand ,@PathVariable Integer id){
        brand.setId(id);
        brandService.updateBrand(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /*
    * 删除品牌
    * */

    @DeleteMapping("/{id}")
    public Result deleteBrand( @PathVariable Integer id){
    brandService.delete(id);
    return new Result(true,StatusCode.OK,"删除成功");
    }


    /*
    * 品牌列表条件查询
    * */
    @GetMapping("/search")
    public Result search(@RequestParam Map searchMap){
        List list = brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);

    }

    /**
     * 品牌列表分页查询
     * page:当前的页码
     * size:每页显示多少条
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")int page,@PathVariable("size") int size){
        Page<Brand> pageInfo = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap,@PathVariable("page")int page,@PathVariable("size") int size){
    int i = 1/0; //测试统一异常处理类
        Page pageInfo = brandService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }




}
